﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace A911CareerKit.Common
{
    public class LoadSaveSupport<T>
    {
        /// <summary>
        /// Saves collection
        /// </summary>
        /// <param name="filePath"></param>
        protected void Save(string filePath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                serializer.Serialize(writer, this);
            }
        }

        protected static T Load(string filePath)
        {
            if (!File.Exists(filePath))
                throw new FileNotFoundException(string.Format("Impossible to find file {0}", filePath));

            XmlSerializer serializer = new XmlSerializer(typeof(T));

            using (FileStream stream = new FileStream(filePath, FileMode.Open))
            {
                return (T)serializer.Deserialize(stream);
            }
        }
    }
}
